package com.zelin.dao.impl;

import com.zelin.dao.StudentDao;
import com.zelin.pojo.Student;
import com.zelin.util.Appliction;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: ZhangXiaoHan
 * @Date: 2020/09/26/20:13
 * @Description:
 */
public class StudentDaoImpl implements StudentDao {
    @Override
    public List<Student> find() {
        List<Student> students = new ArrayList<>();
        //1.1)得到连接
        Connection conn = null;
        try {
            conn= Appliction.getConn();
            //1.2)得到命令对象
            PreparedStatement ps = conn.prepareStatement("select * from student");
            //1.3)执行命令
            ResultSet rs = ps.executeQuery();
            //1.4)将rs转换为list集合
            while(rs != null && rs.next()){
                //1.4.1)从结果集中得到数据
              String stuno =rs.getString("stuno");
              String stuname =rs.getString("stuname");
              String classno =rs.getString("classno");
              String sex =rs.getString("sex");
              int age =rs.getInt("age");
                Student student = new Student(stuno,stuname,classno,sex,age);
                System.out.println(student);
                //1.4.3)将学生对象添加到集合中
                students.add(student);
            }
            //1.5)关闭资源
            Appliction.closeAll(ps,rs,conn);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //1.6)返回
        return students;
    }
}
